adata_dir='N:\AnalyzedData\georg\';
tmp_data_dir='E:\RawData\';

AV_data=struct;

[expxlsnum,expxlstxt]=xlsread('P:\LabLog\Experiments.xls');
ExpIDs=expxlsnum(expxlsnum(:,8)==1|expxlsnum(:,8)==10,1);

layers=expxlsnum(expxlsnum(:,8)==1|expxlsnum(:,8)==10,7);
same_site_as=expxlsnum(expxlsnum(:,8)==1|expxlsnum(:,8)==10,3);
ACode=expxlsnum(expxlsnum(:,8)==1|expxlsnum(:,8)==10,8);

for ind=1:length(ExpIDs)
    ExpID=ExpIDs(ind);
    disp('--------------------------------------------')
    disp(['Now loading Exp ' num2str(ExpID) ' - ' num2str(ind) ' of ' num2str(length(ExpIDs))]);
    disp('--------------------------------------------')
    
    [adata_file,mouse_id,date_id]=find_adata_file(ExpID,adata_dir);
    load([adata_dir mouse_id '\' date_id '\' adata_file]);
    
    aux_data=[];
    aud_data=[];
    imeta_data=[];
    idata=uint8([]);
    nbr_iframes=[];
    indices={};
    
    for knd=1:length(aux_files)
        disp(['loading file ' aux_files{knd} ' from local tmpdata directory']);
        curr_load_path=[tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{knd}];
        tmp_data=load_lvd(curr_load_path);
        aux_data(:,end+1:end+length(tmp_data))=tmp_data;
        
        curr_load_path=[tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{knd}(1:end-4) '.eye'];
        [tmp_idata, tmp_imeta_data] = load_eye_monitor_data(curr_load_path,1);
        nbr_iframes(knd)=length(tmp_imeta_data);
        imeta_data(:,end+1:end+length(tmp_imeta_data))=tmp_imeta_data;
        
        if ExpID==7440
            tmp_idata=tmp_idata(end-249:end,1:250,:);
        end
        if knd==1||sum(size(idata(:,:,1))==size(tmp_idata(:,:,1)))==2
            idata(:,:,end+1:end+size(tmp_idata,3))=tmp_idata;
        else
            disp('Idata sizes don''t match - will improvise');
            idata(1:size(tmp_idata,1),1:size(tmp_idata,2),end+1:end+size(tmp_idata,3))=tmp_idata;
        end
        
        curr_load_path=[tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{knd}(1:end-4) '.aud'];
        [tmp_aud_data, aud_scanrate] = load_aud(curr_load_path);
        aud_data(:,end+1:end+length(tmp_aud_data))=tmp_aud_data;
    end
    
    frame_channel=3;
    
    aux_data=fix_aux_data_M_PB(aux_data,ExpID);
    frame_times=get_frame_times(aux_data(3,:));
    [velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
    
    %%% saccade analysis %%%

    [saccade_frames, pupil_diam, pupil_pos]=find_saccades(idata);
    [iframe_times, shutter_open]=get_iframe_times(imeta_data,aux_data(frame_channel+1,:),nbr_iframes);
    % % %     saccade_times{ind,1}=iframe_times(saccade_frames);
    % % %     saccade_times{ind,2}=type(round(iframe_times(saccade_frames)/10));
    
    % % %     running_hist(ind,:)=hist(velP_raw,[-2:0.01:+2]);
    % % %     type_fract(ind,:)=hist(type,[0:3])/length(type);
    
    whisking=find_whisking(imeta_data);
    if length(aud_data>1)
        comp_aud=compress_aud_data(aud_data,aux_data(frame_channel+1,:));
    else
        comp_aud=NaN;
    end
    raw_act=[];
    for sel_roi=1:length(ROIs)
        act=ftfil(ROIs(sel_roi).activity,18,0,10);
        act=psmooth(act);
        raw_act(:,sel_roi)=act/median(act);
        if strcmp(ROIs(sel_roi).type,'c')
            cell_type(sel_roi)=1;
        else
            cell_type(sel_roi)=0;
        end
    end
    
    
    for knd=1:length(ROIs)
        indices{knd}=ROIs(knd).indices;
    end
    
    AV_data(ind).ExpID=ExpID;
    AV_data(ind).mouse_id=mouse_id;
    AV_data(ind).layer=layers(ind);
    AV_data(ind).same_site_as=same_site_as(ind);
    AV_data(ind).ACode=ACode(ind);
    AV_data(ind).pupil_pos=pupil_pos;
    AV_data(ind).pupil_diam=pupil_diam;
    AV_data(ind).saccade_frames=saccade_frames;
    AV_data(ind).velP=velP;
    AV_data(ind).velM=velM;
    AV_data(ind).velP_ind=velP_ind;
    AV_data(ind).velM_ind=velM_ind;
    AV_data(ind).frame_times=frame_times;
    AV_data(ind).iframe_times=iframe_times;
    AV_data(ind).PS=aux_data(1,:);
    AV_data(ind).shutter_open=shutter_open;
    AV_data(ind).raw_act=raw_act;
    AV_data(ind).cell_type=cell_type;
    AV_data(ind).template=template;
    AV_data(ind).act_map=act_map;
    AV_data(ind).indices=indices;
    AV_data(ind).dx=dx;
    AV_data(ind).dy=dy;
    AV_data(ind).whisking=whisking;
    AV_data(ind).comp_aud=comp_aud;
end


